#include "dialog_svehicle.h"
#include "ui_dialog_svehicle.h"
#include <QRegExpValidator>

// 车辆状态
Dialog_Svehicle::Dialog_Svehicle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Dialog_Svehicle)
{
    ui->setupUi(this);

    SERIALPORTCONFIG_INSTANCE->getSerialPortInfo(m_vecSerialInfo);
    SERIALPORTCONFIG_INSTANCE->getSettingInfo(m_settingInfo);

    // 关闭信号槽
    connect(ui->closeBtn,SIGNAL(clicked()),this,SLOT(close()));

    // 串口号
    strName = m_vecSerialInfo[1].name;
    if(SERIALPORTCTRL_INSTANCE->isOpened(strName)){
        ui->serialportBtn->setText("关闭串口");
    }

    // 正则表达式
    QRegExp regxName("^[0-9]+$");
    QValidator *validator = new QRegExpValidator(regxName);

    PSvehicleInfo info = m_settingInfo.m_psvehicleInfo;

    // 页面初始化

}

Dialog_Svehicle::~Dialog_Svehicle()
{
    delete ui;
}

// 操作串口
void Dialog_Svehicle::on_serialportBtn_clicked()
{
    // 打开串口
    if(ui->serialportBtn->text() == "打开串口"){
        // 获取页面值

        PSvehicleInfo info;
        // 保存值

        PSettingInfo settingInfo;
        settingInfo.m_psvehicleInfo = info;
        // 保存数据
        SERIALPORTCONFIG_INSTANCE->setSettingInfo(settingInfo,strName);
        SERIALPORTCONFIG_INSTANCE->getSettingInfo(m_settingInfo);

        // 打开串口
        SERIALPORTCTRL_INSTANCE->open_port(strName);
        ui->serialportBtn->setText("关闭串口");
    }else{
        // 关闭串口
        SERIALPORTCTRL_INSTANCE->close_port(strName);
        ui->serialportBtn->setText("打开串口");
    }
}

